package {

	/**
	Document class for YouTubeIntegrationDemo 
	
	@author Matthew Richmond <matthew@choppingblock.com>
	@version 1.0
	@history 2008-10-07
	
	@Copyright 2008 Matthew Richmond <matthew@choppingblock.com>
    * 
	* This file is part of Sawdust, a collection of useful frameworks
	* managed by the folks at The Chopping Block, Inc.
    * 
	* Sawdust is free software: you can redistribute it and/or modify
	* it under the terms of the GNU Lesser General Public License as published by
	* the Free Software Foundation, either version 3 of the License, or
	* (at your option) any later version.
    * 
	* Sawdust is distributed in the hope that it will be useful,
	* but WITHOUT ANY WARRANTY; without even the implied warranty of
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	* GNU Lesser General Public License for more details.
    * 
	* You should have received a copy of the GNU Lesser General Public License
	* along with Sawdust.  If not, see <http://www.gnu.org/licenses/>.
	
	@usage
	Activate with:
		<code>
	
		</code>
	
	*/

	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import fl.controls.Button;
	import fl.controls.List;
	import timber.demo.*;
	import choppingblock.video.*;
	
	public class YouTubeIntegrationDemo extends Sprite {

		private var _youTubeLoader:YouTubeLoader;
		
		private var _list:List;					// for demo
		private var _controls:DemoControls;		// for demo
		private var _stateField:TextField;		// for demo

		// ------------------------------------
		// CONSTRUCTOR
		// ------------------------------------

		public function YouTubeIntegrationDemo () {
			trace("YouTubeIntegrationDemo");
			
			// Just for this demo: main frame and header
			var h:DemoHeader = new DemoHeader("ActionScript 3 YouTube Wrapper")
			addChild(h);
			
			createDemoControls();		// Builds out the demo chrome
			create();					// Creates the YouTube loader object
		};

		// ------------------------------------
		// ACCESSORS
		// ------------------------------------

		// ------------------------------------
		// INIT METHODS
		// ------------------------------------

		// ------------------------------------
		// CREATE / DESTROY
		// ------------------------------------
		
		/**
		Creates an instance of the YouTubeLoader class and adds it to the display stack
		*/
		private function create ():void{
			//trace("YouTubeIntegrationDemo: create");

			_youTubeLoader = new YouTubeLoader(); 
			_youTubeLoader.addEventListener(YouTubeLoaderEvent.LOADED, youTubePlayerLoadedHandler, false, 0, true);
			_youTubeLoader.addEventListener(YouTubeLoaderEvent.STATE_CHANGE, youTubePlayerStateChangeHandler, false, 0, true);
			_youTubeLoader.create();
			_youTubeLoader.x = 290;
			_youTubeLoader.y = 27;
			addChild(_youTubeLoader);
			
		};
		
		/**
		Creates all the UI for the demo itself (including a list of test videos), not necessary outside of this implementation.
		*/
		private function createDemoControls ():void{
			//trace("YouTubeIntegrationDemo: createButtonData");
			
			DemoControls.ALLOW_DISABLED = true;
			
			// position vars 
			var posX:Number = 20;
			
			// List title
			var listTitle:TextField = DemoControls.createSetTitle("Videos:");
			listTitle.x = posX;
			listTitle.y = 25;
			addChild(listTitle);
			
			// List
			_list = new List();
			
			_list.addItem({label:"YouTube API: Upload, Player APIs and more!", data:"u1zgFlCw8Aw"});
			_list.addItem({label:"TMBG: Oranges and Graphic Design Live", data:"KmZK2z_d6Nk"});
			_list.addItem({label:"Oktapodi HQ", data:"VaM9mrnOMJo"});
			_list.addItem({label:"An Eye For Annai", data:"RpspcBlOBDY"});
			_list.setSize(250, 80);
			_list.x = posX;
			_list.y = 50;
			_list.enabled = false;
			addChild(_list);
			_list.addEventListener(Event.CHANGE, youtubeLoadVideoHandler);
			
			// buttons
			var d:XML = new XML();
			d = <ui>
				<buttons title="Player Controls" >
					<button title="Scale Up" handler="youtubeSetSizeUpHandler" args="" />
					<button title="Scale Down" handler="youtubeSetSizeDownHandler" args="" />
					<button title="Unload" handler="youtubeClearHandler" args="" />
				</buttons>
				<buttons title="Video Controls" >
					<button title="Play" handler="youtubePlayHandler" args="" />
					<button title="Pause" handler="youtubePauseHandler" args="" />
				</buttons>
				<buttons title="Volume Controls" >
					<button title="Volume 100" handler="youtubeVolumeHandler" args="100" />
					<button title="Volume 50" handler="youtubeVolumeHandler" args="50" />
				</buttons>
				<buttons title="Mute Controls" >
					<button title="Mute" handler="youtubeMuteHandler" args="" />
					<button title="UnMute" handler="youtubeUnMuteHandler" args="" />
				</buttons>
			</ui>
			
			_controls = new DemoControls(this, d)
			_controls.x = posX;
			_controls.y = 140;
			addChild(_controls);
		
			// Player State Title
			var stateTitle:TextField = DemoControls.createSetTitle("Player State:");
			stateTitle.x = posX;
			stateTitle.y = 375;
			addChild(stateTitle);
			
			// Player State Field
			var format:TextFormat = new TextFormat()
			format.font = "Arial";
			format.color = 0x666666;
			format.size = 24;

			_stateField = new TextField()
			_stateField.width = 250;
			_stateField.height = 80;
			_stateField.selectable = false;
			_stateField.border = true;
			_stateField.background = true;
			_stateField.antiAliasType = AntiAliasType.ADVANCED;
			_stateField.defaultTextFormat = format;
			
			_stateField.x = posX;
			_stateField.y = 398;
			addChild(_stateField);
		};
		
		// ------------------------------------
		// EVENT METHODS
		// ------------------------------------
		
		/**
		Called via player loaded event, lets you know you are all clear to send player commands.
		*/
		private function youTubePlayerLoadedHandler (event:YouTubeLoaderEvent):void{
			//trace("YouTubeIntegrationDemo: youTubePlayerLoadedHandler");
			_list.enabled = true;
			_controls.enabled = true;
		};
		
		/**
		Called every time the player state changes
		*/
		private function youTubePlayerStateChangeHandler (event:YouTubeLoaderEvent):void{
			//trace("YouTubeIntegrationDemo: youTubePlayerStateChangeHandler");
			_stateField.text = event.state;
		};
		
		public function youtubeLoadVideoHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeLoadVideoHandler");
			_youTubeLoader.loadVideoById(event.target.selectedItem.data)
		};
		
		public function youtubeSetSizeUpHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeSetSizeUpHandler");
			_youTubeLoader.setSize(620, 465)
		};
		
		public function youtubeClearHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeClearHandler");
			// i have found it's best to stop then clear the video
			_youTubeLoader.stop()
			_youTubeLoader.clearVideo()
		};
		
		public function youtubeSetSizeDownHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeSetSizeDownHandler");
			_youTubeLoader.setSize(320, 240)
		};
		
		public function youtubePlayHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubePlayHandler");
			_youTubeLoader.play()
		};
		
		public function youtubePauseHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubePauseHandler");
			_youTubeLoader.pause()
		};
		
		public function youtubeVolumeHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeVolumeHandler");
			_youTubeLoader.setVolume(Number(event.target.args));
		};
		
		public function youtubeMuteHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeMuteHandler");
			_youTubeLoader.mute();
		};
		
		public function youtubeUnMuteHandler (event:Event):void{
			//trace("YouTubeIntegrationDemo: youtubeUnMuteHandler");
			_youTubeLoader.unMute();
		};
		
	};
};